<template>
  <div class="goods-list" style="width:100%">
    <div class="header">
      <el-menu background-color="#eee" text-color="#303133" class="el-menu-demo" mode="horizontal" router>
        <el-menu-item index="/home">首页</el-menu-item>
        <el-menu-item>商品列表</el-menu-item>
        <el-menu-item>商品信息</el-menu-item>
      </el-menu>
    </div>
    <!-- 卡片视图 -->
    <div style="margin: 20px">
      <el-card style="margin-top:20px">
        <el-form :model="form" inline size="mini" ref="queryRef">
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="所属店铺" prop="store_name">
            <el-input v-model="form.store_name"></el-input>
          </el-form-item>
          <el-form-item label="商品类别" prop="type">
            <el-select v-model="form.type" placeholder="选择分类">
              <el-option v-for="(item, index) in types" :label="item.name" :value="item.name" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onQuery">查询</el-button>
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card style="margin-top:20px">
        <el-table :row-style="{ padding: 0 }" size="mini" :fit="false" :data="tableData">
          <el-table-column prop="store-num" label="商品编号"> </el-table-column>
          <el-table-column prop="store-name" label="所属店铺"> </el-table-column>
          <el-table-column label="商品图">
            <template slot-scope="scope">
              <img class="goods_img" :src="scope.row.photo" />
            </template>
          </el-table-column>
          <el-table-column prop="name" label="商品名称"> </el-table-column>
          <el-table-column prop="type" label="商品类型"> </el-table-column>
          <el-table-column prop="cost" label="成本价"> </el-table-column>
          <el-table-column prop="original" label="原价"> </el-table-column>
          <el-table-column prop="price" label="销售价"> </el-table-column>
          <el-table-column prop="stock" label="库存量"> </el-table-column>
          <el-table-column prop="monthly_sales" label="月销量"> </el-table-column>
          <el-table-column prop="total_sales" label="总销量"> </el-table-column>
          <el-table-column prop="monthly_vol" label="月销售额"> </el-table-column>
          <el-table-column prop="total_vol" label="总销售额"> </el-table-column>
          <el-table-column label="参与活动">
            <template slot-scope="scope">
              <span>{{ scope.row.event ? '是' : '否' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="热销">
            <template slot-scope="scope">
              <span>{{ scope.row.hot ? '是' : '否' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="推荐">
            <template slot-scope="scope">
              <span>{{ scope.row.recommend ? '是' : '否' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              <span>{{ scope.row.status > 0 ? '上架' : '下架' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" round @click="changeStatus(scope.row)">
                {{ scope.row.status > 0 ? '下架' : '上架' }}
              </el-button>
              <el-button size="mini" type="danger" round @click="deletegoods(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>
<script>
export default {
  name: 'goodsList',
  data() {
    return {
      types: [],
      form: {
        name: '',
        store_name: '',
        type: ''
      },
      tableData: []
    }
  },
  methods: {
    async onQuery() {
      // 如果没有选择或者输入任何条件, 查询所有数据
      if (this.form.store_name + this.form.name + this.form.type === '') {
        return
      }
      // 先将数据清空
      this.tableData = []
      // 按照编号发送请求进行模糊查询
      if (this.form.name !== '') {
        const { data: res } = await this.$http.get(`/goods/goods?name_like=${this.form.name}`)
        this.tableData = res
        // 按照名称发送请求进行模糊查询
        const { data: res2 } = await this.$http.get(`/goods/store?store-name_like=${this.form.store_name}`)
        res2.forEach(item => {
          if (!this.tableData.find(subitem => subitem.id === item.id)) {
            this.tableData.push(item)
          }
        })
      }
      if (this.form.type !== '') {
        // 按照行业发送请求进行模糊查询
        const { data: res3 } = await this.$http.get(`/goods?type=${this.form.type}`)
        res3.forEach(item2 => {
          if (!this.tableData.find(subitem => subitem.id === item2.id)) {
            this.tableData.push(item2)
          }
        })
      }
    },
    onReset() {
      this.$refs.queryRef.resetFields()
    },
    async getGoodsList() {
      const { data: res } = await this.$http.get('/goods')
      this.tableData = res
    },
    async getTypeList() {
      const { data: res } = await this.$http.get('/goodtypes')
      this.types = res
    },
    async changeStatus(row) {
      const id = row.id
      let status = null
      var str = ''
      if (row.status > 0) {
        status = 0
        str = '下架'
      } else {
        status = 1
        str = '上架'
      }
      this.$confirm(`确认${str}该商品?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          row.status = status
          const data = await this.$http.put(`/goods/${id}`, row)
          if (data.status === 200) {
            this.$message({ type: 'success', message: '操作成功!' })
            this.getGoodsList()
          } else {
            this.$message.error('操作失败')
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async deletegoods(row) {
      const id = row.id
      this.$confirm('确认删除该商品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await this.$http.delete(`/goods/${id}`)
          if (data.status === 200) {
            this.$message({ type: 'success', message: '操作成功!' })
            this.getGoodsList()
          } else {
            this.$message.error('操作失败')
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  created() {
    this.getGoodsList()
    this.getTypeList()
  }
}
</script>
<style lang="less" scope>
.el-table {
  font-size: 12px;
}
.goods_img {
  height: 50px;
}
</style>
